<?php

class Model_User_Token extends Model_Dec {

	const TABLE = "user_token";
	
	/**
	 * @var protected $id id value
	 * @method int get_id() get_id(void)
	 * @method void set_id() set_id(int $id)
	 */
	protected $id = 0;

	/**
	 * @var protected $user_id id value
	 * @method int get_user_id() get_user_id(void)
	 * @method void set_user_id() set_user_id(int $user_id)
	 */
	protected $user_id = 0;

	/**
	 * @var protected $user_agent user_agent value
	 * @method string get_user_aget() get_user_agent(void)
	 * @method void set_user_agent() set_user_agent(string $user_agent) 
	 */
	protected $user_agent;

	/**
	 * @var protected $token token value
	 * @method string get_token() get_token(void)
	 * @method void set_token() set_token(string $token) 
	 */
	protected $token;

	/**
	 * @var protected $created created value
	 * @method string get_created() get_created(void)
	 * @method void set_created() set_created(string $created) 
	 */
	protected $created;

	/**
	 * @var protected $expires expires value
	 * @method string get_expires() get_expires(void)
	 * @method void set_expires() set_expires(string $expires) 
	 */
	protected $expires;

	/**
	 * @return Model_User_Token
	 */
	public static function factory($id=0) {
		$db = DB::select("id", "user_id", "user_agent", "token", "created", "expires")->from(self::TABLE)->where("id", "=", $id)->limit(1);
		return parent::factory($db);
	}

	public static function get_all($limit = false) {
		$db = DB::select("id", "user_id", "user_agent", "token", "created", "expires")->from(self::TABLE);
		if ($limit)	$db->limit($limit);
		return parent::factory($db);
	}

	private static function get_database_fields() {
		return array(
			'user_id' => __("User_Token_Name"),
			'user_agent' => __("User_Token_User_Agent"),
			'token' => __("User_Token_Token"),
			'created' => __("User_Token_Created"),
			'expires' => __("User_Token_Expires"),
		);
	}
	
	public function validate() {
		$validateArray = array();
		foreach(self::get_data_base_fields() as $key=>$value){
			$validateArray[$key] = $this->get($key);
		}

		$validation = Validation::factory($validateArray)
				->rule('user_id','not_empty')
				->rule('user_agent','not_empty')
				->rule('token','not_empty')
				->rule('created','not_empty')
				->rule('expires','not_empty');
		
		if($validation->check()) {
			return $validateArray;
		} else {
			throw new Validation_Exception($validation->errors());
		}
	}
	
} // End User Token